package yqt.com.cnki.service.impl;


import haha.life.common.CutPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yqt.com.cnki.bo.QBZXBo;
import yqt.com.cnki.dao.IQBZXDao;
import yqt.com.cnki.po.KeyValuePo;
import yqt.com.cnki.po.QBZXPo;
import yqt.com.cnki.service.IQBZXService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Description:
 *
 * @author BaiGe
 * @date: 2021/11/10 15:15
 * @Version 1.0
 */
@Service
public class QBZXServiceImpl implements IQBZXService {
    @Autowired
    private IQBZXDao qbzxDao;

    @Override
    public QBZXPo getQBZXById(Integer sysID) {
        return qbzxDao.getQBZXById(sysID);
    }

    @Override
    public List<KeyValuePo> getSortNameFromDB() {
        return qbzxDao.getSortNameFromDB();
    }

    @Override
    public List<QBZXPo> getQBZXBySortName_LimitFive(QBZXBo qbzxBo) {
        return qbzxDao.getQBZXBySortName_LimitFive(qbzxBo);
    }

    @Override
    public CutPageBean<QBZXPo> getQBZXBySortName_CutPage(QBZXBo qbzxBo) {
        String[] sortArr = qbzxBo.getSortArr();
        StringBuffer sortArrStr = new StringBuffer();
        if (sortArr != null && sortArr.length > 0) {
            for (int i = 0; i < sortArr.length; i++) {
                sortArrStr.append("分类 = ").append(sortArr[i]).append(" or ");
            }
            if (sortArrStr.length() > 3) {
                String ortArrStr = sortArrStr.substring(0, sortArrStr.length() - 3);
                qbzxBo.setSortArrStr(ortArrStr);
            }
        }
        qbzxBo.setStart(qbzxBo.getPage(), qbzxBo.getPageSize());
        CutPageBean<QBZXPo> cutPageBean = new CutPageBean<>();
        List<QBZXPo> list = qbzxDao.getQBZXBySortName_List(qbzxBo);
        int count = (int) qbzxDao.getQBZXBySortName_Count(qbzxBo);
        cutPageBean.initCutPage(count, qbzxBo.getPageSize(), list);
        return cutPageBean;
    }

    @Override
    public List<Map<String, String>> getCover(String source) {
        String realSource = "(";
        String[] split = source.split(",");
        for (String s : split) {
            realSource += "来源 ='" + s + "' or ";
        }
        realSource = realSource.substring(0, realSource.length() - 3)+")";
        List<QBZXPo> cover = qbzxDao.getCover(realSource);
        if (cover == null || cover.isEmpty()) {//为空直接返回
            return null;
        }
        List<Map<String, String>> list = new ArrayList<>();
        String regex = "<img [^>]*src=[''\"']([^ '\"]+)[^>]*>";
        for (QBZXPo qbzxPo : cover) {
            String text = qbzxPo.getText();
            Pattern pattern = Pattern.compile(regex);
            Matcher m = pattern.matcher(text);
            if (m != null && m.find()) { //每条记录只截取1张
                Map<String, String> map = new HashMap<>();
                map.put("id", qbzxPo.getSysId());
                map.put("title", qbzxPo.getTitle());
                map.put("coverAddress", m.group(1));
                map.put("tableCode", qbzxPo.getTableCode());
                list.add(map);
//                list.add(m.group(1));  //只取第一张图片路径
            }
//            while (m.find()) { 截取每一张
//                int i = 1;
//                list.add(m.group(i));
//                i++;
//            }
        }
        return list;
    }
}
